package com.liuwei.spring.cloud.biz.service.impl;/**
 * @author : MockServiceImpl
 * @version V1.0
 * @date: 2024/2/28 10:23 AM
 * @Description: TODO
 */

import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.liuwei.spring.cloud.biz.service.MockService;
import com.liuwei.spring.cloud.framework.constants.RedisCache;
import com.liuwei.spring.cloud.common.utils.DateHelper;
import com.liuwei.spring.cloud.common.utils.Jackson;
import com.liuwei.spring.cloud.framework.annotations.RedisLock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author : MockServiceImpl
 * @version V1.0
 * @date: 2024/2/28 10:23 AM
 * @Description: TODO
 */
@Slf4j
@Service
public class MockServiceImpl implements MockService {

    @Override
    @Cacheable(key="'spring:cache:mock:'+#name",cacheNames = {RedisCache.REDIS_TTL_30_SECONDS},cacheManager=RedisCache.CACHE_MANAGER)
    //@Cacheable(key="'spring:cache:mock:'+#name",cacheNames = {CaffeineCache.CAFFEINE_TTL_30_MINUTES},cacheManager=CaffeineCache.CACHE_MANAGER)
    @RedisLock(prefix = "spring:lock:name:", lockKey = "#name", lockIterator = false)
    @Retryable(value = {Exception.class}, maxAttempts = 3, backoff = @Backoff(delay = 2000L, multiplier = 2))
    public String mock(String name) throws InterruptedException {
        //Thread.sleep(1000*60*5);
        ObjectNode creator = JsonNodeFactory.instance.objectNode();
        creator.put("name",name);
        creator.put("time", DateHelper.formatNosplit24Second(new Date()));
        log.info(Jackson.toJSONString(creator));
        return Jackson.toJSONString(creator);
    }

    @Override
    //@Cacheable(key="#name",cacheNames = {RedisCache.TTL_5_MINUTES})
    @RedisLock(prefix = "redisson:lock:name:", lockKey = "#name", lockIterator = false)
    public String collection(List<String> name) throws InterruptedException {
        Thread.sleep(1000*60*5);
        ObjectNode creator = JsonNodeFactory.instance.objectNode();
        creator.put("names",Jackson.toJSONString(name));
        creator.put("time", DateHelper.formatNosplit24Second(new Date()));
        log.info(Jackson.toJSONString(creator));
        return Jackson.toJSONString(creator);
    }

}
